package com.hcb.micro.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Advertisement;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.service.IAdvertisementService;
import com.hcb.micro.service.IMallService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("advertisement")
public class AdvertisementController extends BaseControllers{
	
	@Autowired
	IMallService mallService;
	
	@Autowired
	IAdvertisementService advertisementService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		
		List<Advertisement> aListAll = advertisementService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<Advertisement> aList = advertisementService.selectByPara(para);
		
		if(aList.size() <= 0){
			json.put("result", "1");
			json.put("description", "广告区列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(Advertisement a:aList){
			Map<String,String> map = new HashMap<>();
			
			map.put("id", a.getId()+"");
			map.put("image1", a.getImage1()==null?"":a.getImage1());
			map.put("image2", a.getImage2()==null?"":a.getImage2());
			map.put("image3", a.getImage3()==null?"":a.getImage3());
			map.put("url1", a.getUrl1()==null?"":a.getUrl1());
			map.put("url2", a.getUrl2()==null?"":a.getUrl2());
			map.put("url3", a.getUrl3()==null?"":a.getUrl3());
			
			String type = "";
			if(a.getType() == 1){
				type = "上";
			}else if(a.getType() == 2){
				type = "中";
			}else if(a.getType() == 3){
				type = "下";
			}
			
			map.put("type", type);
			map.put("createTime", a.getCreateTime()==null?"":sdf.format(a.getCreateTime()));
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取广告区列表成功");
		json.put("list", list);
		json.put("count", aListAll.size());
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "detail", method = RequestMethod.POST)
	@ResponseBody
	public String detail() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("advertisementId") == null || "".equals(bodyInfo.get("advertisementId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		Advertisement a = advertisementService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("advertisementId")));
		
		if(a == null){
			json.put("result", "1");
			json.put("description", "广告区不存在");
			return buildReqJsonObject(json);
		}
		
		Map<String,Object> map = new HashMap<>();
		map.put("id", a.getId()+"");
		map.put("image1", a.getImage1()==null?"":a.getImage1());
		map.put("image2", a.getImage2()==null?"":a.getImage2());
		map.put("image3", a.getImage3()==null?"":a.getImage3());
		map.put("url1", a.getUrl1()==null?"":a.getUrl1());
		map.put("url2", a.getUrl2()==null?"":a.getUrl2());
		map.put("url3", a.getUrl3()==null?"":a.getUrl3());
		map.put("type", a.getType());
		map.put("createTime", a.getCreateTime()==null?"":sdf.format(a.getCreateTime()));
		
    	json.put("result", "0");
		json.put("description", "获取广告区成功");
		json.put("advertisement", map);
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "modify", method = RequestMethod.POST)
	@ResponseBody
	public String modify() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("advertisementId") == null || "".equals(bodyInfo.get("advertisementId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Advertisement advertisement = advertisementService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("advertisementId")));
		
		if(advertisement == null){
			json.put("result", "1");
			json.put("description", "广告曲不存在");
			return buildReqJsonObject(json);
		}
		
		if(bodyInfo.get("image1") != null && !"".equals(bodyInfo.get("image1"))) {
			advertisement.setImage1(bodyInfo.getString("image1"));
		}
		if(bodyInfo.get("image2") != null && !"".equals(bodyInfo.get("image2"))) {
			advertisement.setImage2(bodyInfo.getString("image2"));
		}
		if(bodyInfo.get("image3") != null && !"".equals(bodyInfo.get("image3"))) {
			advertisement.setImage3(bodyInfo.getString("image3"));
		}
		if(bodyInfo.get("url1") != null && !"".equals(bodyInfo.get("url1"))) {
			advertisement.setUrl1(bodyInfo.getString("url1"));
		}
		if(bodyInfo.get("url2") != null && !"".equals(bodyInfo.get("url2"))) {
			advertisement.setUrl2(bodyInfo.getString("url2"));
		}
		if(bodyInfo.get("url3") != null && !"".equals(bodyInfo.get("url3"))) {
			advertisement.setUrl3(bodyInfo.getString("url3"));
		}
		if(bodyInfo.get("type") != null && !"".equals(bodyInfo.get("type"))) {
			advertisement.setType(Byte.valueOf(bodyInfo.getString("type")));
		}
		
		Integer rs = advertisementService.updateByPrimaryKeySelective(advertisement);
		if(rs != 1){
			json.put("result", "1");
			json.put("description", "修改广告区失败");
		}else{
			json.put("result", "0");
			json.put("description", "修改广告区成功");
		}
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Advertisement advertisement = new Advertisement();
		
		if(bodyInfo.get("image1") != null && !"".equals(bodyInfo.get("image1"))) {
			advertisement.setImage1(bodyInfo.getString("image1"));
		}
		if(bodyInfo.get("image2") != null && !"".equals(bodyInfo.get("image2"))) {
			advertisement.setImage2(bodyInfo.getString("image2"));
		}
		if(bodyInfo.get("image3") != null && !"".equals(bodyInfo.get("image3"))) {
			advertisement.setImage3(bodyInfo.getString("image3"));
		}
		if(bodyInfo.get("url1") != null && !"".equals(bodyInfo.get("url1"))) {
			advertisement.setUrl1(bodyInfo.getString("url1"));
		}
		if(bodyInfo.get("url2") != null && !"".equals(bodyInfo.get("url2"))) {
			advertisement.setUrl2(bodyInfo.getString("url2"));
		}
		if(bodyInfo.get("url3") != null && !"".equals(bodyInfo.get("url3"))) {
			advertisement.setUrl3(bodyInfo.getString("url3"));
		}
		if(bodyInfo.get("type") != null && !"".equals(bodyInfo.get("type"))) {
			advertisement.setType(Byte.valueOf(bodyInfo.getString("type")));
		}
		advertisement.setCreateTime(new Date());
		
		Integer rs = advertisementService.insertSelective(advertisement);
		if(rs != 1){
			json.put("result", "1");
			json.put("description", "新增广告区失败");
		}else{
			json.put("result", "0");
			json.put("description", "新增广告区成功");
			json.put("advertisementId", advertisement.getId());
		}
		return buildReqJsonObject(json);
	}
	
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete() throws ParseException {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int right = 1;
		if(bodyInfo.get("advertisementId") == null || "".equals(bodyInfo.get("advertisementId"))) {
			right = 0;
		}
		if(right == 0){
			json.put("result", "1");
			json.put("description", "操作失败，请检查输入的参数是否完整");
			return buildReqJsonObject(json);
		}
		
		Advertisement advertisement = advertisementService.selectByPrimaryKey(Integer.valueOf(bodyInfo.getString("advertisementId")));
		
		if(advertisement == null){
			json.put("result", "1");
			json.put("description", "广告区不存在");
			return buildReqJsonObject(json);
		}
		
		Integer rs = advertisementService.deleteByPrimaryKey(advertisement.getId());
		if(rs != 1){
			json.put("result", "1");
			json.put("description", "删除广告区失败");
		}else{
			json.put("result", "0");
			json.put("description", "删除广告区成功");
		}
		return buildReqJsonObject(json);
	}
}
